<?php
/**
 * 问题接口
 * 提问问题，采纳问题，回复问题
 * 验证用户身份token
 * @author Edwin
 */

namespace V1\Controller;

use Common\Controller\ApiController;

class QuestionController extends ApiController
{
    /**
     * rest路由
     */
    public function rest()
    {
        switch ($this->_method) {
            case 'get':
                $this->_get();
                break;
            case 'post': // post请求处理代码
                $this->_post();
                break;
            case 'put': // put请求处理代码
                break;
        }
    }

    /**
     * 同意回答
     */
    private function _get()
    {
        $from_uid = $this->userId;
        $data = D('QuestionAnswer')->set_agree(I('get.to_uid'), I('get.status'), $from_uid, I('get.id'));
        if (I('get.status') == 1) {
            D('MemberProfile')->add_score(I('get.to_uid'), C('SCORE')['sa_agree_times'], 1);
        }
        if ($data != false) {
            $this->myApiPrint(C('STATUS')['200'], 200, $data);
        } else {
            $this->myApiPrint(C('STATUS')['220'], 201, []);
        }
    }

    /**
     * 提问问题
     */
    private function _post()
    {
        if (!S('userid_' . $this->userId)) {
            $info['uid'] = $this->userId;
            $info['content'] = I('post.content');
            $info['cat_id'] = I('post.cat_id');
            $info['gold'] = !empty(I('post.gold')) ? I('post.gold') : 0;
            $info['thumb0'] = !empty(I('post.thumb0')) ? I('post.thumb0') : '';
            $info['thumb1'] = !empty(I('post.thumb1')) ? I('post.thumb1') : '';
            $info['thumb2'] = !empty(I('post.thumb2')) ? I('post.thumb2') : '';
            $info['thumb3'] = !empty(I('post.thumb3')) ? I('post.thumb3') : '';
            $info['thumb4'] = !empty(I('post.thumb4')) ? I('post.thumb4') : '';
            $info['thumb5'] = !empty(I('post.thumb5')) ? I('post.thumb5') : '';
            $info['location'] = !empty(I('post.location')) ? trim(I('post.location')) : '';
            //通过会员id获取会员金币
            $gold = D('MemberProfile')->get_member_gold_int($this->userId);
            if ($gold < $info['gold']) {
                $this->myApiPrint('您的金额不足,请充值', 400);
            } else {
                //问题内容写入数据库
                if (D('Ask')->add_quick_ask($info)) {
                    //1分钟内禁止频繁提交
                    S('userid_' . $this->userId, 'ok', 10);
                    //会员积分累加
                    D('MemberProfile')->add_score($this->userId, 'add_question');
                    D('MemberProfile')->scrubtract_gold($this->userId, $info['gold']);
                    $k = "user_ask_image_" . $this->userId;
                    $k1 = $k . "_ask_tmp_image";
                    S($k1, null);
                    $this->myApiPrint('提问成功', 200);
                } else {
                    $this->myApiPrint('提问失败,请稍后再试', 201);
                }
            }
        } else {
            $this->myApiPrint(C('STATUS')['225'], 400);
        }
    }

    /**
     * 采纳问题
     */
    public function setAnswerAdopt()
    {
        $info['to_uid'] = I('post.to_uid');
        $info['from_uid'] = I('post.from_uid');
        $info['askid'] = I('post.askid');
        $info['answerid'] = I('post.answerid');
        //判断是否已经采纳
        if (D('QuestionAdopt')->check_answer_adopt($info['askid']) > 0) {
            $this->myApiPrint(C('STATUS')['217'], 400);
        }
        //获取该问题悬赏金币额度
        $gold_num = D('QuestionAsk')->gold_num($info['askid']);
        //问题采纳
        if (D('QuestionAdopt')->add_answer_adopt($info)) {
            D('MemberProfile')->add_gold($info['to_uid'], intval($gold_num['gold']));       //回复问题者 加金币
            D('MemberProfile')->minus_gold($info['from_uid'], intval($gold_num['gold']));   //提问问题者 减金币
            $this->myApiPrint(C('STATUS')['200'], 200);
        } else {
            $this->myApiPrint(C('STATUS')['215'], 201);
        }
    }

    /**
     * 回复问题
     */
    public function addQuestionAnswer()
    {
        $info = I('post.');
        $info['uid'] = !empty($info['post.uid']) ? $info['post.uid'] : $this->userId;
        $resultId = D('QuestionAnswer')->addQuestionAnswer($info);
        if ($resultId > 0) {
            //统计回复消息数top_ier
            D('Ask')->get_ask_number($resultId);
            //积分累加
            D('MemberProfile')->add_score($info['uid'], 'sa_answer');
            //回复消息设置
            D('Message')->add_message_reply($info);
            $this->myApiPrint(C('STATUS')['200'], 200);
        } else {
            $this->myApiPrint(C('STATUS')['215'], 201);
        }


    }

    /**
     * 添加图片
     */
    public function addImage()
    {
        $stu_id['stu_id'] = $this->userId;
        $data = [];
        for ($i = 0; $i < 6; $i++) {
            $data[$i] = base64imgsave(I("post.img" . $i), $this->userId, 'ask', $i);
        }
        if ($data === false) {
            //添加出错
            $this->myApiPrint('图片保存失败，请稍后再试', 201, []);
        } else {
            $this->myApiPrint('success', 200, $data);
        }
    }
}